package su.mvc.action;

import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyFactory;
import com.googlecode.objectify.ObjectifyOpts;
import com.opensymphony.xwork2.Preparable;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.util.ServletContextAware;
import su.mvc.service.gui.GuiService;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

public class ActionSupport extends com.opensymphony.xwork2.ActionSupport implements
        ServletRequestAware,ServletResponseAware, Preparable, ServletContextAware {
    
    public static final String GUI_SERVICE = "guiService";
    public static final String CURRENT_PAGE = "currentUrl";
    public static final String OBJECTIFY = "objectify";
    public static final String IMAGE = "imageResult";
    
    public static final String REDIRECT = "redirect";
    public static final String REDIRECT_OK = "redirect_ok";
    public static final String REDIRECT_CANCEL = "redirect_cancel";
    
    public static final String ACCESS_DENIED = "access-denied";

    
    private GuiService guiService;

    private Objectify objectify;

    public Objectify getObjectify() {
        return objectify; 
    }

    public void setObjectify(Objectify objectify) {
        this.objectify = objectify;
    }

    public void addError(String error) {
        addActionError(getText(error));
    }

    public GuiService getGuiService() {
        return guiService;
    }

    public void setGuiService(GuiService guiService) {
        this.guiService = guiService;
    }

    public HttpServletRequest request;
    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }
    public HttpServletResponse response;
    public void setServletResponse(HttpServletResponse response) {
        this.response = response;
    }

    public ServletContext context;
    public void setServletContext(ServletContext context) {
        this.context = context;
    }
    
    public String getCurrentUrl(){
        StringBuilder url = new StringBuilder(ServletActionContext.getActionMapping().getNamespace() );
        if(url.length()>1) url.append("/");
        url.append(ServletActionContext.getActionMapping().getName());
        if (ServletActionContext.getActionMapping().getMethod() != null) {
            url.append("!").append(ServletActionContext.getActionMapping().getMethod());
        }
        return url.toString();
    }

    @Override
    public void prepare() throws Exception {
        request.setAttribute(GUI_SERVICE, getGuiService());
        request.setAttribute(CURRENT_PAGE, getCurrentUrl());
        ObjectifyFactory objectifyFactory = getGuiService().getObjectifyFactory();
        ObjectifyOpts opts = new ObjectifyOpts().setSessionCache(true);
        Objectify objectify = objectifyFactory.begin(opts);
        request.setAttribute(OBJECTIFY, objectify);
        this.objectify = objectify;

        if (request.getParameter("errorMessages") !=null) {
            List<String> errorMessages = new ArrayList<String>() ;
            for (String errorMessage: request.getParameterValues("errorMessages")){
                errorMessages.add(getText(errorMessage));
            }
            setActionErrors(errorMessages);
        }

        if (request.getParameter("messages") != null) {
            List<String> messages = new ArrayList<String>();
            for (String message: request.getParameterValues("messages")){
                messages.add(getText(message));
            }
            setActionMessages(messages);
        }
        
    }


}
